#include "prec.h"
#include "artbitmapbutton.h"
#include "artpanel.h"
#include "wx/log.h"
#include "wx/dcbuffer.h"

IMPLEMENT_DYNAMIC_CLASS(ArtBitmapButton, wxControl)

BEGIN_EVENT_TABLE(ArtBitmapButton, wxControl)
	EVT_PAINT(ArtBitmapButton::OnPaint)
	EVT_ERASE_BACKGROUND(ArtBitmapButton::OnEraseBackground)
	EVT_SIZE(ArtBitmapButton::OnSize)
	EVT_LEFT_DOWN(ArtBitmapButton::OnLeftDown)
	EVT_LEFT_UP(ArtBitmapButton::OnLeftUp)
	EVT_LEAVE_WINDOW(ArtBitmapButton::OnMouseLeave)
	EVT_ENTER_WINDOW(ArtBitmapButton::OnMouseEnter)
	EVT_SET_FOCUS(ArtBitmapButton::OnGainFocus)
	EVT_KILL_FOCUS(ArtBitmapButton::OnLoseFocus)
	EVT_LEFT_DCLICK(ArtBitmapButton::OnLeftDown)
END_EVENT_TABLE()

ArtBitmapButton::ArtBitmapButton( ArtPanel *parent, wxWindowID id, 
					 const wxString& label,
					 const wxBitmap& bitmap, 
					 const wxBitmap& hoverBitmap,
					 const wxPoint& pos /*= wxDefaultPosition*/, 
					 const wxSize& size /*= wxDefaultSize*/, 
					 long style /*= 0*/, 
					 const wxString& name /*= wxStaticTextNameStr*/ )
					 : wxControl(parent, id, pos, size, style | wxNO_BORDER, wxDefaultValidator, name)
					 , m_artPanel(parent)
{
	m_bitmap = bitmap;
	m_hoverBitmap = hoverBitmap;

	wxControl::SetLabel(label);
	InheritAttributes();
	SetInitialSize(size);
}

void ArtBitmapButton::SetBitmap( const wxBitmap& bitmap )
{
	m_bitmap = bitmap;
	Refresh();
}

void ArtBitmapButton::OnPaint( wxPaintEvent& event )
{
	wxBufferedPaintDC dc(this);

	dc.SetBackground(wxBrush(GetParent()->GetBackgroundColour()));
	dc.Clear();

	wxBitmap bmp = m_artPanel->GetBackgroundRegion(GetId());
	if (bmp.Ok())
	{
		dc.DrawBitmap(bmp, 0, 0, false);
	}

	wxRect clientRect = GetClientRect();
	wxWindow* capture = wxWindow::GetCapture();

	int x = clientRect.x, y = clientRect.y, width = clientRect.width, height = clientRect.height;

	dc.SetFont(GetFont());
	dc.SetPen(wxPen(GetForegroundColour()));

	wxString label = GetLabel();
	wxSize tsize = dc.GetTextExtent(label);
	int tw = tsize.x, th = tsize.y;

	if (capture != this)
	{
		if (m_bitmap.Ok())
			dc.DrawBitmap(m_bitmap, (width-m_bitmap.GetWidth())/2, (height-m_bitmap.GetHeight())/2, true);
	}
	else
	{
		if (m_hoverBitmap.Ok())
			dc.DrawBitmap(m_hoverBitmap, (width-m_hoverBitmap.GetWidth())/2, (height-m_bitmap.GetHeight())/2, true);
	}

	dc.DrawText(label, (width-tw)/2, (height-th)/2);
}

void ArtBitmapButton::OnSize( wxSizeEvent& event )
{
	event.Skip();
	Refresh();
}

void ArtBitmapButton::OnEraseBackground( wxEraseEvent& event )
{

}

wxSize ArtBitmapButton::DoGetBestSize() const
{
	return wxSize(m_bitmap.GetWidth(), m_bitmap.GetHeight());
}

void ArtBitmapButton::SetLabel( const wxString &label )
{
	wxControl::SetLabel(label);
	Refresh();
}

void ArtBitmapButton::OnLeftDown( wxMouseEvent& event )
{
	if(!IsEnabled())
		return;

	m_mouseAction = CLICK;
	CaptureMouse();
	Refresh();
	event.Skip();
}

void ArtBitmapButton::OnLeftUp( wxMouseEvent& event )
{
	if (!IsEnabled() || !HasCapture())
		return;

	wxPoint pos = event.GetPosition();
	wxRect rect = GetClientRect();

	if (HasCapture())
		ReleaseMouse();

	if (rect.Contains(pos))
	{
		m_mouseAction = HOVER;
		Notify();
	}
	else
	{
		m_mouseAction = 0;
	}

	Refresh();
	event.Skip();
}

void ArtBitmapButton::OnMouseLeave( wxMouseEvent& event )
{
	m_mouseAction = None;
	Refresh();
	event.Skip();
}

void ArtBitmapButton::OnMouseEnter( wxMouseEvent& event )
{
	if (!IsEnabled())
		return;

	m_mouseAction = HOVER;
	Refresh();
	event.Skip();
}

void ArtBitmapButton::OnGainFocus( wxFocusEvent& event )
{
	m_hasFocus = true;
	Refresh();
	Update();
}

void ArtBitmapButton::OnLoseFocus( wxFocusEvent& event )
{
	m_hasFocus = false;
	Refresh();
	Update();
}

bool ArtBitmapButton::AcceptsFocus() const
{
	return IsShown() && IsEnabled();
}

wxVisualAttributes ArtBitmapButton::GetDefaultAttributes() const
{
	return wxButton::GetClassDefaultAttributes();
}

bool ArtBitmapButton::Enable( bool enable /*= true*/ )
{
	bool ret = wxControl::Enable(enable);
	Refresh();
	return ret;
}

bool ArtBitmapButton::SetForegroundColour( const wxColour& colour )
{
	bool ret = wxControl::SetForegroundColour(colour);
	Refresh();
	return ret;
}

void ArtBitmapButton::SetDefault()
{

}

void ArtBitmapButton::Notify()
{
	ArtBitmapButtonEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
	evt.SetButtonObj(this);
	evt.SetEventObject(this);
	GetEventHandler()->ProcessEvent(evt);
}

void ArtBitmapButton::SetHoverBitmap( const wxBitmap& bitmap )
{
	if (bitmap.Ok())
	{
		m_hoverBitmap = bitmap;
		if (m_mouseAction != None)
			Refresh();
	}
}